module ew {
    export class Loader {
        private imagesToLoad: number;
        private imagesLoaded: number;
        private images: Object;

        constructor() {
            this.imagesLoaded = 0;
            this.imagesToLoad = 0;
            this.images = new Object();
        }

        public done: Function;
        public error: Function;

        public loadImage(imageName: string, path: string) {
            if (this.images[imageName] == undefined) {
                var image = new Image();
                image.src = path;
                this.images[imageName] = image;
                this.imagesToLoad++;

                var self = this;
                image.onload = function () {
                    self.imagesLoaded++;
                    if (self.imagesLoaded >= self.imagesToLoad)
                        self.done();
                }
                image.onerror = function () {
                    self.imagesToLoad--;
                    self.error(this.src);
                };
            }
        }
    }
}